package com.bluetrum.abmate.bluetooth.adapter;

import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.lifecycle.LifecycleOwner;
import androidx.recyclerview.widget.RecyclerView;

import com.bluetrum.abmate.R;
import com.bluetrum.abmate.databinding.DeviceItemBinding;
import com.bluetrum.abmate.viewmodels.ScannerLiveData;
import com.bluetrum.devicemanager.models.ABDevice;

import java.util.List;
import java.util.Locale;

public class DevicesAdapter extends RecyclerView.Adapter<DevicesAdapter.ViewHolder> {

    private final List<ABDevice> mDevices;
    private OnItemClickListener mOnItemClickListener;

    public DevicesAdapter(final LifecycleOwner lifecycleOwner, final ScannerLiveData scannerLiveData) {
        this.mDevices = scannerLiveData.getDevices();
        scannerLiveData.observe(lifecycleOwner, devices -> {
            final Integer i = devices.getUpdatedDeviceIndex();
            if (i != null)
                notifyItemChanged(i);
            else
                notifyDataSetChanged();
        });
    }

    public void setOnItemClickListener(@NonNull final OnItemClickListener listener) {
        mOnItemClickListener = listener;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull final ViewGroup parent, final int viewType) {
        DeviceItemBinding binding = DeviceItemBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false);
        return new ViewHolder(binding);
    }

    @Override
    public void onBindViewHolder(@NonNull final ViewHolder holder, final int position) {
        final ABDevice device = mDevices.get(position);
        final String deviceName = device.getBleName();

        if (!TextUtils.isEmpty(deviceName)) {
            holder.deviceName.setText(deviceName);
        } else {
            holder.deviceName.setText(R.string.unknown_name);
        }
        holder.deviceAddress.setText(device.getBleAddress());
        final int rssiPercent = (int) (100.0f * (127.0f + device.getRssi()) / (127.0f + 20.0f));
        holder.deviceRssi.setImageLevel(rssiPercent);
    }

    @Override
    public long getItemId(final int position) {
        return position;
    }

    @Override
    public int getItemCount() {
        return mDevices.size();
    }

    public boolean isEmpty() {
        return getItemCount() == 0;
    }

    @FunctionalInterface
    public interface OnItemClickListener {
        void onItemClick(final ABDevice device);
    }

    final class ViewHolder extends RecyclerView.ViewHolder {
        private final TextView deviceAddress;
        private final TextView deviceName;
        private final ImageView deviceRssi;

        private ViewHolder(final DeviceItemBinding binding) {
            super(binding.getRoot());
            deviceAddress = binding.deviceAddress;
            deviceName = binding.deviceName;
            deviceRssi = binding.deviceRssi;

            binding.deviceContainer.setOnClickListener(view -> {
                if (mOnItemClickListener != null) {
                    if(getAdapterPosition() > -1) {
                        mOnItemClickListener.onItemClick(mDevices.get(getAdapterPosition()));
                    }
                }
            });
        }
    }

}
